﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using testIP;

namespace RYTools
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private double lastPosLeft = 0;
        private double lastPosTop = 0;
        private DispatcherTimer dispatcherTimer;

        public MainWindow()
        {
            InitializeComponent();
            this.Background = Brushes.Transparent;


            lastPosLeft = Convert.ToInt16(ConfigHelper.GetAppConfig("Left"));
            lastPosTop = Convert.ToInt16(ConfigHelper.GetAppConfig("Top"));

            dispatcherTimer = new DispatcherTimer();
        }

        public void dispatcherTimer_Tick(object sender, EventArgs e)
        {
            IP.Text = GetIP.GetHostIP();
            if (Left != lastPosLeft || Top != lastPosTop)
            {
                lastPosLeft = Left;
                lastPosLeft = Top;

                ConfigHelper.UpdateAppConfig("Top", this.Top.ToString());
                ConfigHelper.UpdateAppConfig("Left", this.Left.ToString());
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if ((lastPosLeft == 0) && (lastPosTop == 0))
            {
                double x = SystemParameters.WorkArea.Width;//得到屏幕工作区域宽度
                double y = SystemParameters.WorkArea.Height;//得到屏幕工作区域高度
                this.Left = x - this.Width;
                this.Top = 0;

            }
            else
            {
                Top = lastPosTop;
                Left = lastPosLeft;
            }
            

            String2Display callDuration = new String2Display();

            IP.DataContext = callDuration;

            callDuration.Text = GetIP.GetHostIP();

            dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
            dispatcherTimer.Interval = new TimeSpan(0, 0, 10);
            dispatcherTimer.Start(); 

        }

        public static string GetIPAddress()
        {
            string userIP = "未获取用户IP";


            return userIP;
        }

        private static IntPtr s_hWndOldParent = IntPtr.Zero;

        private bool CheckParent(IntPtr hWnd)
        {

            IntPtr hWndProgram = IntPtr.Zero;
            IntPtr hWndShellDLL = IntPtr.Zero;
            IntPtr hDeskWnd = IntPtr.Zero;


            hWndProgram = User32API.FindWindow("WorkerW", "");
            while (hWndProgram != IntPtr.Zero)
            {
                hWndShellDLL = User32API.FindWindowEx(hWndProgram, IntPtr.Zero, "SHELLDLL_DefView", null);
                if (hWndShellDLL != IntPtr.Zero)
                {
                    hDeskWnd = User32API.FindWindowEx(hWndShellDLL, IntPtr.Zero, "SysListView32", null);
                    break;
                }
                hWndProgram = User32API.GetWindow(hWndProgram, User32API.GW_HWNDNEXT);
            }

            if (hDeskWnd == IntPtr.Zero)
            {
                hDeskWnd = User32API.FindWindow("Progman", "Program Manager");
                if (hDeskWnd != IntPtr.Zero)
                {
                    hDeskWnd = User32API.FindWindowEx(hDeskWnd, IntPtr.Zero, "SHELLDLL_DefView", null);
                    hDeskWnd = User32API.FindWindowEx(hDeskWnd, IntPtr.Zero, "SysListView32", null);

                }
            }

            if ((hWndShellDLL != IntPtr.Zero) && hWndShellDLL != s_hWndOldParent)
            {
                User32API.SetWindowLong(hWnd, GWL.GWL_HWNDPARENT, (int)hDeskWnd);
                User32API.SetWindowPos(hWnd, 1, 0, 0, 0, 0, User32API.SE_SHUTDOWN_PRIVILEGE);
                s_hWndOldParent = hWndShellDLL;
                return true;
            }

            return false;
        }


        protected override void OnRender(DrawingContext dc)
        {
            base.OnRender(dc);
            HwndSource hs = (HwndSource)PresentationSource.FromDependencyObject(this);
            IntPtr ip = hs.Handle;
            CheckParent(ip);
            User32API.SetWindowPos(ip, 1, 0, 0, 0, 0, 1 | 2 | 0x10 | 0x40);
        }

        private void LayoutRoot_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                this.DragMove();
            }
        }
    }
}
